<?php

namespace backend\helpers;

class FileNameInflector extends \yii\helpers\BaseInflector
{
    public static function specialSlug($string, $replacement = '-', $lowercase = true)
    {
        if (empty($string)) {
            return (string) $string;
        }

        $parts = explode($replacement, $string);

        $replaced = array_map(function ($element) use ($replacement) {
            $element = preg_replace('/[^a-zA-Zа-яА-Я0-9=\s—–-]+/u', $replacement, $element);
            return preg_replace('/[=—–-]+/u', $replacement, $element);
        }, $parts);

        $string = trim(implode($replacement, $replaced), $replacement);
        if ((string)$replacement !== '') {
            $string = preg_replace('#' . preg_quote($replacement, '#') . '+#', $replacement, $string);
        }
        
        return $string;
    }
}